package com.wangshuo.webadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshuo.webadmin.common.ItemType;
import com.wangshuo.webadmin.entity.*;
import com.wangshuo.webadmin.mapper.*;
import com.wangshuo.webadmin.service.*;
import com.wangshuo.webadmin.vo.apartment.ApartmentDetailVo;
import com.wangshuo.webadmin.vo.apartment.ApartmentItemVo;
import com.wangshuo.webadmin.vo.apartment.ApartmentQueryVo;
import com.wangshuo.webadmin.vo.apartment.ApartmentSubmitVo;
import com.wangshuo.webadmin.vo.fee.FeeValueVo;
import com.wangshuo.webadmin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 13326
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2025-01-21 16:43:50
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;


    @Override
    public  void saveOrUpdateapart(ApartmentSubmitVo apartmentSubmitVo){
        boolean isUpdate=apartmentSubmitVo.getId()!=null;
        super.saveOrUpdate(apartmentSubmitVo);
        if(isUpdate){
            //1.删除图片列表
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            lambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);
            //2.删除配套列表
            LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(lambdaQueryWrapper1);
            //3.删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper=new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelLambdaQueryWrapper);
            //4.删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> lambdaQueryWrapper2=new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(lambdaQueryWrapper2);
           //插入图片列表
            List<GraphVo> list=apartmentSubmitVo.getGraphVoList();
            if(!CollectionUtils.isEmpty(list)){
                ArrayList<GraphInfo> graphInfos=new ArrayList<>();
                for (GraphVo graphVo:list){
                    GraphInfo graphInfo=new GraphInfo();
                  graphInfo.setItemType(1);
                  graphInfo.setItemId(apartmentSubmitVo.getId());
                  graphInfo.setName(graphVo.getName());
                  graphInfo.setUrl(graphVo.getUrl());
                  graphInfos.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfos);
            }
            //插入配套列表
            List<Long> facilitylist=apartmentSubmitVo.getFacilityInfoIds();
            if(!CollectionUtils.isEmpty(facilitylist)){
                ArrayList<ApartmentFacility> list1=new ArrayList<>();
                for (Long facilityId:facilitylist){
                    ApartmentFacility apartmentFacility=new ApartmentFacility();
                    apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFacility.setFacilityId(facilityId);
                    list1.add(apartmentFacility);
                }
                apartmentFacilityService.saveBatch(list1);
            }
            //插入标签列表
            List<Long> labelIds=apartmentSubmitVo.getLabelIds();
            if(!CollectionUtils.isEmpty(labelIds)){
                List<ApartmentLabel> list2=new ArrayList<>();
                for (Long labelId:labelIds){
                    ApartmentLabel apartmentLabel=new ApartmentLabel();
                    apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                    apartmentLabel.setLabelId(labelId);
                    list2.add(apartmentLabel);
                }
                apartmentLabelService.saveBatch(list2);
            }
            //插入杂费列表
            List<Long> list3=apartmentSubmitVo.getFeeValueIds();
            if(!CollectionUtils.isEmpty(list3)){
                ArrayList<ApartmentFeeValue> apartmentFeeValueArrayList=new ArrayList<>();
                for (Long feeValueId:list3){
                    ApartmentFeeValue apartmentFeeValue=new ApartmentFeeValue();
                    apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFeeValue.setFeeValueId(feeValueId);
                    apartmentFeeValueArrayList.add(apartmentFeeValue);
                }
                apartmentFeeValueService.saveBatch(apartmentFeeValueArrayList);
            }



        }

    }
    @Override
    public IPage<ApartmentItemVo> pageApartmernt(IPage<ApartmentItemVo> page, ApartmentQueryVo apartmentQueryVo){
        return apartmentInfoMapper.pageApartment(page,apartmentQueryVo);
    }
    @Override
    public ApartmentDetailVo getdetail(Long id){
      ApartmentInfo apartmentInfo=this.getById(id);
      if(apartmentInfo == null){
          return null;
      }
      List<GraphVo> graphVoList=graphInfoMapper.selectlistbyid(1,id);
      List<LabelInfo> labelinfolist=labelInfoMapper.selectlistbyid(id);
      List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectlistbyid(id);
      List<FeeValueVo> feeValueVoList=feeValueMapper.selectlistbyid(id);
      ApartmentDetailVo apartmentDetailVo=new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

      apartmentDetailVo.setFacilityInfoList(facilityInfoList);
      apartmentDetailVo.setGraphVoList(graphVoList);
      apartmentDetailVo.setFeeValueVoList(feeValueVoList);
      apartmentDetailVo.setLabelInfoList(labelinfolist);
      return  apartmentDetailVo;

    }
@Override
    public String deleteByid(Long id){
        LambdaQueryWrapper<RoomInfo> apartmentInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long count=roomInfoMapper.selectCount(apartmentInfoLambdaQueryWrapper);
        if(count >0){
//throw new IOException("请先删除公寓下的房间");
return "请先删除公寓下的房间";
        }else{
            super.removeById(id);
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getId,id);
            lambdaQueryWrapper.eq(GraphInfo::getItemType,1);
            graphInfoService.remove(lambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(lambdaQueryWrapper1);

            LambdaQueryWrapper<ApartmentLabel> lambdaQueryWrapper2=new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(lambdaQueryWrapper2);

            LambdaQueryWrapper<ApartmentFeeValue> lambdaQueryWrapper3=new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(lambdaQueryWrapper3);


        }


    return null;
}

}




